package com.example.wenhua.controller;

import com.example.wenhua.common.Result;
import com.example.wenhua.dto.StudentQueryRequest;
import com.example.wenhua.dto.StudentCreditsSummary;
import com.example.wenhua.entity.Student;
import com.example.wenhua.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生管理控制器
 */
@RestController
@RequestMapping("/api/students")
@CrossOrigin(origins = "*")
public class StudentController {
    
    @Autowired
    private StudentService studentService;
    
    /**
     * 查询所有学生
     */
    @GetMapping
    public Result<List<Student>> getAllStudents() {
        try {
            List<Student> students = studentService.getAllStudents();
            return Result.success(students);
        } catch (Exception e) {
            return Result.error("查询学生列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据学号查询学生
     */
    @GetMapping("/{sno}")
    public Result<Student> getStudentBySno(@PathVariable String sno) {
        try {
            Student student = studentService.getStudentBySno(sno);
            if (student != null) {
                return Result.success(student);
            } else {
                return Result.error("学生不存在");
            }
        } catch (Exception e) {
            return Result.error("查询学生失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据系名查询学生
     */
    @GetMapping("/dept/{dept}")
    public Result<List<Student>> getStudentsByDept(@PathVariable String dept) {
        try {
            List<Student> students = studentService.getStudentsByDept(dept);
            return Result.success(students);
        } catch (Exception e) {
            return Result.error("查询学生失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增学生
     */
    @PostMapping
    public Result<Void> addStudent(@RequestBody Student student) {
        try {
            boolean success = studentService.addStudent(student);
            if (success) {
                return Result.success("学生添加成功", null);
            } else {
                return Result.error("学生添加失败");
            }
        } catch (Exception e) {
            return Result.error("学生添加失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新学生信息
     */
    @PutMapping("/{sno}")
    public Result<Void> updateStudent(@PathVariable String sno, @RequestBody Student student) {
        try {
            student.setSno(sno);
            boolean success = studentService.updateStudent(student);
            if (success) {
                return Result.success("学生信息更新成功", null);
            } else {
                return Result.error("学生信息更新失败");
            }
        } catch (Exception e) {
            return Result.error("学生信息更新失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除学生
     */
    @DeleteMapping("/{sno}")
    public Result<Void> deleteStudent(@PathVariable String sno) {
        try {
            boolean success = studentService.deleteStudent(sno);
            if (success) {
                return Result.success("学生删除成功", null);
            } else {
                return Result.error("学生删除失败");
            }
        } catch (Exception e) {
            return Result.error("学生删除失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量导入学生
     */
    @PostMapping("/batch")
    public Result<Void> batchImportStudents(@RequestBody List<Student> students) {
        try {
            boolean success = studentService.batchImportStudents(students);
            if (success) {
                return Result.success("学生批量导入成功", null);
            } else {
                return Result.error("学生批量导入失败");
            }
        } catch (Exception e) {
            return Result.error("学生批量导入失败：" + e.getMessage());
        }
    }
    
    /**
     * 条件查询学生（支持分页）
     */
    @PostMapping("/query")
    public Result<Object> queryStudents(@RequestBody StudentQueryRequest request) {
        try {
            Object result = studentService.queryStudents(request);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询学生失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询学生已修学分总数
     */
    @GetMapping("/{sno}/credits")
    public Result<Integer> getTotalCreditsBySno(@PathVariable String sno) {
        try {
            Integer credits = studentService.getTotalCreditsBySno(sno);
            return Result.success(credits);
        } catch (Exception e) {
            return Result.error("查询学分失败：" + e.getMessage());
        }
    }
    
    /**
     * 查询所有学生的学分统计信息
     */
    @GetMapping("/credits-summary")
    public Result<List<StudentCreditsSummary>> getStudentCreditsSummary() {
        try {
            List<StudentCreditsSummary> summary = studentService.getStudentCreditsSummary();
            return Result.success(summary);
        } catch (Exception e) {
            return Result.error("查询学分统计失败：" + e.getMessage());
        }
    }
}